package com.ggh.jinjilive.ui.mall.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentCommodityManagementBinding;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.shop.adapter.CommodityManagementAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CommodityManagementFragment extends BaseFragment<FragmentCommodityManagementBinding> implements View.OnClickListener, OnRefreshListener, OnLoadMoreListener {
    //VM
    private MineShopViewModel mineShopViewModel;
    //商品管理
    private int status = 1;
    //适配器
    private CommodityManagementAdapter commodityManagementAdapter;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mineShopViewModel = new ViewModelProvider(this,viewModelFactory).get(MineShopViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_commodity_management;
    }

    @Override
    public void initView() {
        super.initView();
        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取商品列表
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
    }

    @Override
    public void initData() {
        super.initData();
        //获取商品列表
        onRefresh(binding.refreshLayout);
        //适配器
        commodityManagementAdapter = new CommodityManagementAdapter();
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(commodityManagementAdapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //出售中
        binding.ButtonOnOffer.setOnClickListener(this);
        //未上架
        binding.ButtonNoGrounding.setOnClickListener(this);
        //添加商品
        binding.llAddGoods.setOnClickListener(this);
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //出售中
        mineShopViewModel.MallGoodsList.observe(this, new Observer<List<MallGoods>>() {
            @Override
            public void onChanged(List<MallGoods> mallGoods) {
                binding.refreshLayout.finishRefresh();
                if(mineShopViewModel.isRefresh){
                    if(mallGoods.size() > 0){
                        loadService.showSuccess();
                        commodityManagementAdapter.setAdd(mallGoods);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(mallGoods.size() > 0){
                        commodityManagementAdapter.ADD(mallGoods);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Button_onOffer://出售中
                //添加商品
                binding.llAddGoods.setVisibility(View.GONE);
                //状态改变
                stateGoods();
                //出售中
                binding.ButtonOnOffer.setTextColor(Color.parseColor("#1890ff"));
                //出售中
                status = 1;
                //获取商品列表
                onRefresh(binding.refreshLayout);
                break;
            case R.id.Button_noGrounding://未上架
                //添加商品
                binding.llAddGoods.setVisibility(View.VISIBLE);
                //状态改变
                stateGoods();
                //未上架
                binding.ButtonNoGrounding.setTextColor(Color.parseColor("#1890ff"));
                //未上架
                status = 0;
                //获取商品列表
                onRefresh(binding.refreshLayout);
                break;
            case R.id.ll_add_goods://添加商品
                ARouter.getInstance().build(ARouterUtils.AddGoodsActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
        }
    }

    /**
     * 状态
     */
    public void stateGoods(){
        //出售中
        binding.ButtonOnOffer.setTextColor(Color.parseColor("#FFFFFF"));
        //未上架
        binding.ButtonNoGrounding.setTextColor(Color.parseColor("#FFFFFF"));
    }


    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取商品列表
        mineShopViewModel.isRefresh = true;
        mineShopViewModel.shopProductList(status);
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取商品列表
        mineShopViewModel.isRefresh = false;
        mineShopViewModel.shopProductList(status);
    }


}
